package middleware

import (
	"fmt"
	"gin_erik/pkg/utils"
	"net/http"

	"github.com/gin-gonic/gin"
)

// ErrorHandler 异常处理中间件
func ErrorHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if r := recover(); r != nil {
				// 处理异常
				errMsg := fmt.Sprintf("%v", r)
				utils.SendResponse(c, http.StatusInternalServerError, "Internal Server Error: "+errMsg, nil)
			}
		}()
		c.Next()
	}
}

func NotFoundHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		utils.SendResponse(c, http.StatusNotFound, "Not Found", nil)
	}
}

func MethodNotAllowedHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		utils.SendResponse(c, http.StatusMethodNotAllowed, "Method Not Allowed", nil)
	}
}
